import * as THREE from 'three';
// 导入data.gui
import * as dat from 'dat.gui';
// 导入轨道控制器
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';

// 目标：灯光与阴影
// 灯光阴影
// 1、 材质要满足能够对光照有反应
// 2、 设置渲染器开启阴影的计算 renderer.shadowMap.enabled = true;
// 3、 设置光照投射阴影 directionalLight.castShadow = true;
// 4、 设置物体投射阴影 sphere.castShadow = true;
// 5、 设置物体接收阴影 plane.receiveShadow = true;

// 1、创建场景
const scene = new THREE.Scene();

// 2、创建相机
const SCREEN_WIDTH = window.innerWidth;
const SCREEN_HEIGHT = window.innerHeight;
const aspect = SCREEN_WIDTH / SCREEN_HEIGHT;
const camera = new THREE.PerspectiveCamera(75, aspect, 0.1, 1000);
// 设置相机的位置
camera.position.set(0, 0, 10);
scene.add(camera);

const sphereGeometry = new THREE.SphereBufferGeometry(1, 20, 20);
const material = new THREE.MeshStandardMaterial();
const sphere = new THREE.Mesh(sphereGeometry, material);
// 投射阴影
sphere.castShadow = true;
scene.add(sphere);

//创建平面
const planeGeometry = new THREE.PlaneBufferGeometry(10, 10);
const plane = new THREE.Mesh(planeGeometry, material);
plane.position.set(0, -1, 0);
plane.rotation.x = -Math.PI / 2;
// 接收阴影
plane.receiveShadow = true;
scene.add(plane);

// 灯光
// 环境光
const light = new THREE.AmbientLight(0xffffff, 0.5);
scene.add(light);
// 聚光灯
const spotLight = new THREE.SpotLight(0xffffff, 0.5);
spotLight.position.set(5, 5, 5);
spotLight.castShadow = true;
// 设置阴影贴图模糊度
spotLight.shadow.radius = 20;
// 设置阴影贴图的分辨率
spotLight.shadow.mapSize.set(4096, 4096);

// 设置透射相机的属性
// directionalLight.shadow.camera.near = 0.5;
// directionalLight.shadow.camera.far = 500;
// directionalLight.shadow.camera.top = 5;
// directionalLight.shadow.camera.bottom = -5;
// directionalLight.shadow.camera.left = -5;
// directionalLight.shadow.camera.right = 5;
scene.add(spotLight);

const gui = new dat.GUI();
gui.add(spotLight.shadow.camera, 'near').min(0).max(10).step(0.1).onChange(() => {
    spotLight.shadow.camera.updateProjectionMatrix();
});

// 初始化渲染器
const renderer = new THREE.WebGL1Renderer();
// 设置渲染的尺寸大小
renderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
// 开启场景中的阴影贴图
renderer.shadowMap.enabled = true;
// 将webgl渲染的canvas内容添加到body
document.body.appendChild(renderer.domElement);
// 创建轨道控制器
const controls = new OrbitControls(camera, renderer.domElement);
// 设置控制器阻尼，让控制器更有真实效果,必须在动画循环里面调用update()
controls.enableDamping = true;

// 添加坐标轴辅助器
const axesHelper = new THREE.AxesHelper(5);
scene.add(axesHelper);

window.addEventListener('dblclick', () => {
    // 双击控制屏幕进入全屏，退出全屏
    if (!document.fullscreenElement) {
        // 让画布对象全屏
        renderer.domElement.requestFullscreen().then();
    } else {
        // 退出全屏，使用document对象
        document.exitFullscreen().then();
    }
});

function render() {
    controls.update();
    renderer.render(scene, camera);
    // 渲染下一帧的时候就会调用animate函数
    requestAnimationFrame(render);
}

render();

// 监听画面变化，更新渲染画面
window.addEventListener('resize', () => {
    // 更新摄像头
    camera.aspect = window.innerWidth / window.innerHeight;
    // 更新摄像机的投影矩阵
    camera.updateProjectionMatrix();
    // 更新渲染器
    renderer.setSize(window.innerWidth, window.innerHeight);
    // 设置渲染器的像素比
    renderer.setPixelRatio(window.devicePixelRatio);
});
